package com.xbfinal.myzhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbfinal.myzhxy.mapper.TeacherMapper;
import com.xbfinal.myzhxy.pojo.LoginForm;
import com.xbfinal.myzhxy.pojo.Student;
import com.xbfinal.myzhxy.pojo.Teacher;
import com.xbfinal.myzhxy.service.TeacherService;
import com.xbfinal.myzhxy.util.MD5;
import com.xbfinal.myzhxy.util.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

@Service("TeacherServiceImpl")
@Transactional
public class TeacherServiceImpl
        extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService{
    public Teacher login(LoginForm loginForm){
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        final Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;

    }

    @Override
    public Teacher getTeacherById(Long userId) {
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<Teacher> getBradeByOpr(Page<Teacher> page, String name) {
        //创建mybatis的查询对象QueryWrapper
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        //先降序排序根据id
        queryWrapper.orderByDesc("id");
        //然后判断 传过来的值是狗为空
        if(!(StringUtils.isEmpty(name))){
            queryWrapper.like("name",name);
        }
        //成功就返回值
        final Page<Teacher> page1 = baseMapper.selectPage(page, queryWrapper);
        return page1;



    }
}
